package org.jdu.dao.wrapper;

import java.io.CharArrayReader;
import java.io.Writer;

import org.jdu.exception.ServiceException;

import oracle.jdbc.driver.OracleClobWriter;
import oracle.sql.CLOB;

import weblogic.jdbc.common.OracleClobImpl;

/**
 * Classe wrapper per Oracle CLOB se si utilizza un connection pool weblogic
 * 
 * @author epelli
 * 
 */

public class WeblogicCLOBWrapper implements LOBWrapper {

	private OracleClobImpl clob;

	public Object getLob() {
		// TODO Auto-generated method stub
		return clob;
	}

	public void setLob(Object lob) {
		this.clob = (OracleClobImpl) lob;
	}

	public void writeLob(Object chars) throws Exception {
		if (!(chars instanceof char[])) {
			throw new ServiceException(
					"L'oggetto lob da inserire deve essere di tipo "
							+ char[].class);
		}
		if (chars != null && clob != null) {
			char[] ch = (char[]) chars;
			CharArrayReader reader = new CharArrayReader(ch);
			Writer lobOutStream = this.clob.getCharacterOutputStream();
			OracleClobWriter writer = (OracleClobWriter) lobOutStream;
//			this.clob.setCharacterStream(ch.length);
			char[] buffer = new char[ch.length];
			int nread = 0;
			int position = 1;
			CLOB _oracleClob = (CLOB) clob.getOracleObject();
			if (clob.length() >= ch.length) {
				_oracleClob.trim(ch.length);
			}
//				lobOutStream.write(buffer, 0, nread);
				clob.putChars(position, ch);
			lobOutStream.flush();
			
		}
		// if(clob.length()>=ch.length){
		// this.clob.truncate(ch.length);
		// }
		// this.clob.putChars(1, (char[]) chars);

	}

}
